package com.yxt.qmgf.home.ctrl;

import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.system.UserInfo;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yxt.qmgf.common.ClientSession;
import com.yxt.qmgf.start.dao.LoginDao;
import com.yxt.qmgf.start.vo.LoginVO;
import com.yxt.qmgf.userinfo.ctrl.UserInfoController;
import com.yxt.qmgf.userinfo.dao.UserInfoDao;
import com.yxt.qmgf.userinfo.vo.UserInfoVO;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;

public class SettingPageController implements Initializable {

    @FXML
    private VBox vbox;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
    }

    // 添加退出账号的方法
    @FXML
    public void logout() {
        // 清除 ClientSession 中的用户信息
        ClientSession.removeAttribute("user");
        ClientSession.removeAttribute("user_info");

        // 隐藏当前主窗口
        Stage currentStage = (Stage) vbox.getScene().getWindow();
        currentStage.close();

        // 加载登录页面
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/welcome.fxml"));
            Parent root = loader.load();
            Scene loginScene = new Scene(root);
            Stage loginStage = new Stage();
            loginStage.setScene(loginScene);
            loginStage.show();

            FXMLLoader userinfo = new FXMLLoader(getClass().getResource("/fxml/UserInfo/UserInfo.fxml"));
            // 重新初始化UserInfoController中的vo对象
            UserInfoController controller = userinfo.getController();
            controller.setOff(null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @FXML
    public void delet(){
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setTitle("确认注销");
        alert.setHeaderText("你确定要注销账号吗？");
        alert.setContentText("此操作不可恢复，请谨慎操作。");

        Optional<ButtonType> result = alert.showAndWait();
        if (result.isPresent() && result.get() == ButtonType.OK) {

            UserInfoVO vo = (UserInfoVO) ClientSession.getAttribute("user_info");
            SpringUtil.getBean(UserInfoDao.class).DeleteByUid(vo.getUid());

            LoginVO loginVO = (LoginVO) ClientSession.getAttribute("user");
            SpringUtil.getBean(LoginDao.class).Delete(String.valueOf(loginVO.getUid()));


            // 清除 ClientSession 中的用户信息
            ClientSession.removeAttribute("user");
            ClientSession.removeAttribute("user_info");

            // 隐藏当前主窗口
            Stage currentStage = (Stage) vbox.getScene().getWindow();
            currentStage.hide();

            // 加载登录页面
            try {
                FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/welcome.fxml"));
                Parent root = loader.load();
                Scene loginScene = new Scene(root);
                Stage loginStage = new Stage();
                loginStage.setScene(loginScene);
                loginStage.show();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}